package com.mengdie.shuidi.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.mengdie.shuidi.AppConfig;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.manager.MDAccountMgr;
import com.mengdie.shuidi.utils.bga.BGABanner;

import java.util.Arrays;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：HQY on 16/11/7 11:41
 * 邮箱：hqy_xz@126.com
 */

public class LeadActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.banner_guide_background)
    BGABanner mBannerGuideBackground;
    @Bind(R.id.btn_guide_enter)
    Button mBtnGuideEnter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lead);
        ButterKnife.bind(this);
        setListener();
        processLogic();
        AppConfig.getInstance().setHasLaunch(true);

    }

    private void setListener() {
        mBtnGuideEnter.setOnClickListener(this);
        // 监听页码切换事件，控制「跳过按钮」和「进入主界面的显示与隐藏」
        mBannerGuideBackground.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == mBannerGuideBackground.getItemCount() - 2) {
                    ViewCompat.setAlpha(mBtnGuideEnter, positionOffset);

                    if (positionOffset > 0.5f) {
                        mBtnGuideEnter.setVisibility(View.VISIBLE);
                    } else {
                        mBtnGuideEnter.setVisibility(View.GONE);
                    }
                } else if (position == mBannerGuideBackground.getItemCount() - 1) {
                    mBtnGuideEnter.setVisibility(View.VISIBLE);
                    ViewCompat.setAlpha(mBtnGuideEnter, 1.0f);
                } else {
                    mBtnGuideEnter.setVisibility(View.GONE);
                }
            }
        });
    }

    private void processLogic() {
        mBannerGuideBackground.setOverScrollMode(View.OVER_SCROLL_NEVER);

        // 初始化方式1：通过传入数据模型并结合Adapter的方式初始化
        mBannerGuideBackground.setAdapter(new BGABanner.Adapter() {
            @Override
            public void fillBannerItem(BGABanner banner, View view, Object model, int position) {
                ((ImageView) view).setImageResource((int) model);
            }
        });
        mBannerGuideBackground.setData(Arrays.asList(R.drawable.lead_start1, R.drawable.lead_start2), null);


    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_guide_enter) {
            MDAccountMgr.getInstance().setLogin(true);
            MDAccountMgr.getInstance().writeToCache(LeadActivity.this);
            startActivity(new Intent(LeadActivity.this, LoginActivity.class));
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 在界面可见时给背景Banner设置一个白色背景，避免滑动过程中两个Banner都设置透明度后能看到Launcher
        mBannerGuideBackground.setBackgroundResource(android.R.color.white);
    }
}
